﻿using System;
using System.Net;
using System.Windows.Forms;

namespace Kay.UdpClient.Demo
{
    public partial class StartUp : Form
    {
        private UdpInterNetworkDgramClient _udpClinet;

        public StartUp()
        {
            InitializeComponent();
        }

        private void StartUp_Load(object sender, EventArgs e)
        {
            _udpClinet = new UdpInterNetworkDgramClient(IPAddress.Any.ToString(), 6666);
            _udpClinet.TextMessageReceived += OnUdpClientTextMessageReceived;
            _udpClinet.Start();
        }

        private void OnUdpClientTextMessageReceived(object sender, TextMessageReceivedEventArgs e)
        {
            if (textBox_Messages.InvokeRequired)
            {
                textBox_Messages.Invoke(new Action<object, TextMessageReceivedEventArgs>(OnUdpClientTextMessageReceived), sender, e);
                return;
            }

            if (textBox_Messages.Text.Length != 0)
            {
                textBox_Messages.AppendText(Environment.NewLine);
                textBox_Messages.AppendText(Environment.NewLine);
            }

            textBox_Messages.AppendText(e.UserInfo.RemoteEndPointByClient + " 说：");
            textBox_Messages.AppendText(Environment.NewLine);
            textBox_Messages.AppendText(e.Text);
        }

        private void StartUp_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_udpClinet != null)
            {
                _udpClinet.Stop();
            }
        }

        private void button_Send_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            var strMessage = textBox_Message.Text.Trim();
            if (strMessage == string.Empty)
            {
                errorProvider.SetError(textBox_Message, "请填写消息内容！");
                return;
            }
            var strIp = textBox_Ip.Text.Trim();
            IPAddress ip;
            if (IPAddress.TryParse(strIp, out ip))
            {
                _udpClinet.SendTextMessage(ip, strMessage);
                if (textBox_Messages.Text.Length != 0)
                {
                    textBox_Messages.AppendText(Environment.NewLine);
                    textBox_Messages.AppendText(Environment.NewLine);
                }

                textBox_Messages.AppendText("我 说：");
                textBox_Messages.AppendText(Environment.NewLine);
                textBox_Messages.AppendText(strMessage);
            }
            else
            {
                errorProvider.SetError(textBox_Ip, "Ip 地址有误！");
            }
        }

        private void button_Broadcast_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            var strMessage = textBox_Message.Text.Trim();
            if (strMessage == string.Empty)
            {
                errorProvider.SetError(textBox_Message, "请填写消息内容！");
                return;
            }
            _udpClinet.SendTextMessage(IPAddress.Broadcast, strMessage);
            if (textBox_Messages.Text.Length != 0)
            {
                textBox_Messages.AppendText(Environment.NewLine);
                textBox_Messages.AppendText(Environment.NewLine);
            }

            textBox_Messages.AppendText("我 广播：");
            textBox_Messages.AppendText(Environment.NewLine);
            textBox_Messages.AppendText(strMessage);
        }
    }
}
